package com.cskaoyan.order.biz.factory;

import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CancelOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.biz.convert.CancelOrderConvert;
import com.cskaoyan.order.biz.convert.TransConvert;
import com.cskaoyan.order.biz.handler.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 取消订单的pipeline工厂
 *
 * @author zhuqiushi
 * @since 2022/09/21 16:15
 */
@Service
@Slf4j
public class OrderCancelProcessPipelineFactory extends AbstranctTransPipelineFactory {

    @Autowired
    private CancelUpdateOrderHandler cancelUpdateOrderHandler;
    @Autowired
    private CancelUpdateOrderItemHandler cancelUpdateOrderItemHandler;
    @Autowired
    private CancelUpdateStockHandler cancelUpdateStockHandler;

    @Override
    public TransOutboundInvoker build(Object obj) {
        //首先创建转换器
        CancelOrderConvert convert = (CancelOrderConvert) createConvert();
        //创建产品
        TransHandlerContext context = createContext();
        //上朔
        AbsTransHandlerContext absCtx = (AbsTransHandlerContext) context;

        //转换
        convert.convertCancelIdToContext((String) obj,context);
        //创建管道
        TransPipeline pipeline = createPipeline(context);
        //build 管道
        doBuild(pipeline);

        return pipeline;
    }

    @Override
    protected TransHandlerContext createContext() {
        CancelOrderContext cancelOrderContext = new CancelOrderContext();
        return cancelOrderContext;
    }

    @Override
    protected void doBuild(TransPipeline pipeline) {
        //首先修改order表
        //在这里对context的order-items属性赋值
        pipeline.addLast(cancelUpdateOrderHandler);
        //再修改order-item表
        pipeline.addLast(cancelUpdateOrderItemHandler);
        //再修改stock表
        pipeline.addLast(cancelUpdateStockHandler);
    }

    @Override
    protected TransConvert createConvert() {
        CancelOrderConvert cancelOrderConvert = new CancelOrderConvert();
        return cancelOrderConvert;
    }
}
